home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / CONTRIB / agv_example.c next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  4.5 KB  |  216 lines

  1. /* 
  2.  * agv_example.c  (version 1.0)
  3.  *
  4.  * Example program to show how to use AGV
  5.  *
  6.  * See agviewer.h, agviewer.c and comments within for more info
  7.  *
  8.  * Philip Winston - 4/11/95
  9.  * pwinston@hmc.edu
  10.  * http://www.cs.hmc.edu/people/pwinston
  11.  */
  12.  
  13. #include <GL/glut.h>
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <math.h>
  18.  
  19. #include "agviewer.h"
  20.  
  21. typedef enum {NOTALLOWED, AXES, STUFF, RING } DisplayLists;
  22.  
  23. int DrawAxes = 0;
  24.  
  25. #define ROTATEINC 2;
  26.  
  27. GLfloat Rotation = 0;  /* start ring flat and not spinning */
  28. int     Rotating = 0;
  29.  
  30.  
  31. void myGLInit(void)
  32. {
  33.   GLfloat mat_ambuse[] = { 0.6, 0.0, 0.0, 1.0 };
  34.   GLfloat mat_specular[] = { 0.4, 0.4, 0.4, 1.0 };
  35.  
  36.   GLfloat light0_position[] = { 0.6, 0.4, 0.3, 0.0 };
  37.  
  38.   glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
  39.   glEnable(GL_LIGHTING);
  40.   glEnable(GL_LIGHT0);
  41.  
  42.   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_ambuse);
  43.   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  44.   glMaterialf(GL_FRONT, GL_SHININESS, 25.0);
  45.   
  46.   glMatrixMode(GL_PROJECTION);
  47.   glLoadIdentity();
  48.   glMatrixMode(GL_MODELVIEW);
  49.   glLoadIdentity();
  50.  
  51.   glEnable(GL_NORMALIZE);
  52.  
  53.   glDepthFunc(GL_LESS);
  54.   glEnable(GL_DEPTH_TEST);
  55.  
  56.   glShadeModel(GL_SMOOTH);
  57.  
  58.   glFlush();
  59.  
  60.  
  61. void MakeDisplayLists(void)
  62. {
  63.   glNewList(STUFF, GL_COMPILE);
  64.   glPushMatrix();
  65.     glutSolidCube(1.0);
  66.     glTranslatef(2, 0, 0);
  67.     glutSolidSphere(0.5, 10, 10);
  68.     glTranslatef(-2, 0, 3);
  69.     glRotatef(-90, 1, 0, 0);
  70.     glutSolidCone(0.5, 1.0, 8, 8);
  71.   glPopMatrix();
  72.   glEndList();
  73.  
  74.   glNewList(RING, GL_COMPILE);
  75.     glutSolidTorus(0.1, 0.5, 8, 15);
  76.   glEndList();
  77. }
  78.  
  79.  
  80. void display(void)
  81. {
  82.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  83.  
  84.   glMatrixMode(GL_PROJECTION);
  85.   glLoadIdentity();
  86.  
  87.   gluPerspective(60, 1, 0.01, 100);
  88.  
  89.     /* so this replaces gluLookAt or equiv */
  90.   agvViewTransform();
  91.  
  92.   glMatrixMode(GL_MODELVIEW);
  93.   glLoadIdentity();
  94.  
  95.     /* we call agvMakeAxesList() to make this display list */
  96.   if (DrawAxes)
  97.     glCallList(AXES);
  98.  
  99.   glCallList(STUFF);
  100.  
  101.   glTranslatef(-2, 1, -2);
  102.   glRotatef(Rotation, 1, 0, 0);
  103.   glCallList(RING);
  104.  
  105.   glutSwapBuffers();
  106.   glFlush();
  107. }
  108.  
  109.  
  110.   /* rotate the axis and adjust position if nec. */
  111. void rotatethering(void)
  112.   Rotation += ROTATEINC;
  113.  
  114.   if (agvMoving)   /* we since we are the only idle function, we must */
  115.     agvMove();     /* give AGV the chance to update the eye position */
  116.  
  117.   glutPostRedisplay();
  118. }
  119.  
  120. typedef enum { MENU_AXES, MENU_QUIT, MENU_RING } MenuChoices;
  121.  
  122. void handlemenu(int value)
  123. {
  124.   switch (value) {
  125.     case MENU_AXES:
  126.       DrawAxes = !DrawAxes;
  127.       break;
  128.     case MENU_QUIT:
  129.       exit(0);
  130.       break;
  131.     case MENU_RING:
  132.       Rotating = !Rotating;
  133.       if (Rotating) {
  134.     glutIdleFunc(rotatethering);    /* install our idle function */
  135.     agvSetAllowIdle(0);             /* and tell AGV to not */
  136.       } else {
  137.     glutIdleFunc(NULL);    /* uninstall our idle function      */
  138.     agvSetAllowIdle(1);    /* and tell AGV it can mess with it */
  139.       }
  140.       break;
  141.     }
  142.   glutPostRedisplay();
  143. }
  144.  
  145. void visible(int v)
  146. {
  147.   if (v == GLUT_VISIBLE) {
  148.     if (Rotating) {
  149.       glutIdleFunc(rotatethering);
  150.       agvSetAllowIdle(0);
  151.     } else {
  152.       glutIdleFunc(NULL);
  153.       agvSetAllowIdle(1);      
  154.     }
  155.   } else {
  156.       glutIdleFunc(NULL);
  157.       agvSetAllowIdle(0);
  158.   }
  159. }
  160.  
  161.  
  162. void MenuInit(void)
  163. {
  164.   int sub2 = glutCreateMenu(agvSwitchMoveMode);   /* pass these right to */
  165.   glutAddMenuEntry("Flying move",  FLYING);       /* agvSwitchMoveMode() */
  166.   glutAddMenuEntry("Polar move",   POLAR);
  167.  
  168.   glutCreateMenu(handlemenu);
  169.   glutAddSubMenu("Movement", sub2);
  170.   glutAddMenuEntry("Toggle Axes", MENU_AXES);
  171.   glutAddMenuEntry("Toggle ring rotation", MENU_RING);
  172.   glutAddMenuEntry("Quit", MENU_QUIT);
  173.   glutAttachMenu(GLUT_RIGHT_BUTTON);
  174. }
  175.  
  176.  
  177. int main(int argc, char** argv)
  178. {
  179.   glutInit(&argc, argv);
  180.   glutInitWindowSize(512, 512);
  181.   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  182.   glutCreateWindow("AGV example");
  183.   
  184.   glutVisibilityFunc(visible);
  185.  
  186.   if (Rotating)
  187.     glutIdleFunc(rotatethering);
  188.  
  189.     /*
  190.      * let AGV know if it can mess with the idle function (if we've
  191.      * just installed an idle function, we tell AGV it can't touch it)
  192.      */
  193.  
  194.   agvInit(!Rotating);
  195.     
  196.     /* 
  197.      * agvInit() installs mouse, motion, and keyboard handles, but 
  198.      * we don't care for this example cause we only use right button menu
  199.      */
  200.  
  201.   agvMakeAxesList(AXES);  /* create AGV axes */
  202.  
  203.   myGLInit(); 
  204.   MakeDisplayLists();
  205.   MenuInit();
  206.  
  207.   glutDisplayFunc(display);
  208.  
  209.   glutMainLoop();
  210.   return 0;             /* ANSI C requires main to return int. */
  211. }
  212.  
  213.  
  214.